<template>
  <j-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="器官名称">
          <a-input placeholder="请输入器官名称" v-decorator="['organName', {rules: [{ required: true, message: '请输入器官名称!' }]}]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="排序">
          <a-input-number v-decorator="[ 'sort', {rules: [{ required: true, message: '请输入器官排序!' }]}]"/>
        </a-form-item>
        <a-form-item label="关联部位" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select
            show-search
            option-filter-prop="children"
            :filter-option="filterOption"
            mode="multiple"
            v-model="bodyPartsInfoList"
            style="width: 100%"
            placeholder="选择关联部位"
            @change="handleChange">
            <a-select-option v-for="item in bodyPartsList"
                             :value="item.id"
                             :key="item.id">
              {{item.bodyPartsName}}
            </a-select-option>
          </a-select>
        </a-form-item>


      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
  import {getAction, httpAction} from '@/api/manage'
  import pick from 'lodash.pick'

  export default {
    name: "MedicalPartsOrganInfoModal",
    data() {
      return {
        title: "操作",
        visible: false,
        model: {},
        labelCol: {
          xs: {span: 24},
          sm: {span: 5},
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 16},
        },
        bodyPartsList: [],
        bodyPartsValueList: [],
        bodyPartsInfoList: [],
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules: {},
        url: {
          add: "/recovery/medicalPartsOrganInfo/add",
          edit: "/recovery/medicalPartsOrganInfo/edit",
          getBodyPartsList: "/recovery/medicalBodyPartsInfo/getBodyPartsList",
        },
      }
    },
    created() {
      this.getBodyPartsList();
    },
    methods: {
      add() {
        this.edit({});
      },
      edit(record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.bodyPartsInfoList = [];
        if (this.model.bodyPartsInfoList && this.model.bodyPartsInfoList.length > 0) {
          this.bodyPartsInfoList = this.model.bodyPartsInfoList;
        }
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'organName', 'sort', 'delFlag','bodyPartsInfoList'))
          //时间格式化
        });
      },
      close() {
        this.$emit('close');
        this.visible = false;
      },
      handleOk() {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if (!this.model.id) {
              httpurl += this.url.add;
              method = 'post';
            } else {
              httpurl += this.url.edit;
              method = 'put';
            }
            this.model.bodyPartsInfoList = this.bodyPartsInfoList;
            let formData = Object.assign(this.model, values);
            //时间格式化
            console.log(formData)
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
        })
      },
      getBodyPartsList() {
        getAction(this.url.getBodyPartsList).then((res) => {
          if (res.success) {
            this.bodyPartsList = res.result;
            console.info("打印参数：{}", this.bodyPartsList)
          }
        });
      },
      handleCancel() {
        this.close()
      },
      handleChange(value) {
        this.bodyPartsValueList = value;
      },
      filterOption(input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        );
      },
    }
  }
</script>

<style lang="less" scoped>

</style>